import {createRouter, createWebHistory} from "vue-router";

const childrens = [
    {
        path: "first",
        name: "first",
        component: () => import("@/view/page/index/index.vue")
    },
    {
        path: "user",
        name: "user",
        component: () => import("@/view/page/user/index.vue")
    },
    {
        path: "generator",
        name: "generator",
        component: () => import("@/view/page/generator/index.vue")
    },
    {
        path: "setting",
        name: "setting",
        component: () => import("@/view/page/generator/setting.vue")
    },
    {
        path: "role",
        name: "role",
        component: () => import("@/view/page/role/index.vue")
    },
    {
        path: "oss",
        name: "oss",
        component: () => import("@/view/page/oss/index.vue")
    },
    {
        path: "pay",
        name: "pay",
        component: () => import("../view/page/pay/index.vue")
    },
    {
        path: "chat",
        name: "chat",
        component: () => import("../view/page/chat/index.vue")
    },
    {
        path: "setting",
        name: "setting",
        component: () => import("../view/home/setting.vue")
    }
]

const route = [
    {
        path: "/admin",
        redirect: "/admin/login"
    },
    {
        path: "/admin/login",
        name: "login",
        component: () => import("@/view/login/index.vue")
    },
    {
        path: "/admin/home",
        name: "home",
        meta: { requiresAuth: true },
        component: () => import((`@/view/home/index.vue`)),
        children: childrens,
    },
    {
        path: "/",
        component: () => import(`@/view/frontendLayout/index.vue`),
        children: [
            {
                path: "",
                name: "index",
                component: () => import(/* webpackChunkName: "home" */ "../view/frontendLayout/main/index.vue")
            },
            {
                path:"articles",
                name:"articles",
                component: () => import(/* webpackChunkName: "articles" */ "../view/frontendLayout/articles/inedx.vue")
            },
            {
                path:"links",
                name:"links",
                component: () => import(/* webpackChunkName: "links" */ "../view/frontendLayout/links/inedx.vue")
            },
        ],
    },
    {
      path: "/payResult",
      name: "payResult",
      component: ()=>import("../view/page/pay/result.vue")
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'not-found',
        component: () => import((`@/components/exception/notFound.vue`)),
        meta: {
            title: '404',
            hidden: true
        }
    }
]

const router = createRouter({
    routes: route,
    history: createWebHistory()
})

// 添加缓存机制
const routeCache = new Map()

router.beforeEach((to, from, next) => {
    console.log("调用路由守卫");
    
    // 检查缓存
    if (routeCache.has(to.path)) {
        console.log('使用缓存的路由:', to.path)
    }
    
    const userInfo = JSON.parse(sessionStorage.getItem("userInfo") || 'null');
    
    if (to.matched.some(record => record.meta.requiresAuth)) {
        if (!userInfo) {
            next({
                path: "/admin/login"
            })
        } else {
            // 缓存已认证的路由
            routeCache.set(to.path, true)
            next()
        }
    } else {
        // 缓存公开路由
        routeCache.set(to.path, true)
        next()
    }
})

export default router;